home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / ubiquity / user-setup / user-setup-apply < prev    next >
Encoding:
Text File  |  2007-04-12  |  4.9 KB  |  186 lines

  1. #! /bin/bash
  2. set -e
  3.  
  4. . /usr/share/debconf/confmodule
  5.  
  6. if [ "$1" ]; then
  7.     ROOT="$1"
  8.     chroot=chroot
  9.     log='log-output -t user-setup'
  10. else
  11.     ROOT=
  12.     chroot=
  13.     log=
  14. fi
  15.  
  16. . /usr/lib/ubiquity/user-setup/functions.sh
  17.  
  18. # Set a password, via chpasswd.
  19. # Use a heredoc rather than echo, to avoid the password
  20. # showing in the process table. (However, this is normally
  21. # only called when first installing the system, when root has no
  22. # password at all, so that should be an unnecessary precaution).
  23. #
  24. # Pass in three arguments: the user, the password, and 'true' if the
  25. # password has been pre-crypted (by preseeding).
  26. setpassword () {
  27.     local USER
  28.     local PASSWD
  29.     local PRECRYPTED
  30.     USER="$1"
  31.     PASSWD="$2"
  32.     if [ "$3" = true ]; then
  33.         PRECRYPTED=-e
  34.     fi
  35.     $chroot $ROOT chpasswd $PRECRYPTED -m <<EOF
  36. $USER:$PASSWD
  37. EOF
  38. }
  39.  
  40. add_to_aliases () {
  41.     if ! grep -qi ^root: $ROOT/etc/aliases 2>/dev/null; then
  42.         cat <<EOF >>$ROOT/etc/aliases
  43. # Added by installer for initial user
  44. root:    $1
  45. EOF
  46.         if [ -x $ROOT/usr/bin/newaliases ]; then
  47.             $log $chroot $ROOT /usr/bin/newaliases
  48.         fi
  49.     fi
  50. }
  51.  
  52. # Enable/disable shadow passwords.
  53. db_get passwd/shadow
  54. if [ "$RET" = true ]; then
  55.     $log $chroot $ROOT shadowconfig on
  56. else
  57.     $log $chroot $ROOT shadowconfig off
  58. fi
  59.  
  60. if ! root_password; then
  61.     # Was the root password preseeded encrypted?
  62.     if db_get passwd/root-password-crypted && [ "$RET" ]; then
  63.         # The root password was preseeded encrypted.
  64.         ROOT_PW="$RET"
  65.         PRECRYPTED=true
  66.     else
  67.         db_get passwd/root-password
  68.         ROOT_PW="$RET"
  69.         PRECRYPTED=false
  70.     fi
  71.     # Clear the root password from the database, and set the password.
  72.     db_set passwd/root-password-crypted ''
  73.     db_set passwd/root-password ''
  74.     db_set passwd/root-password-again ''
  75.     if [ "$ROOT_PW" ]; then
  76.         setpassword root "$ROOT_PW" "$PRECRYPTED"
  77.     fi
  78.     ROOT_PW=
  79. else
  80.     # Just in case, clear any preseeded root password from the database
  81.     # anyway.
  82.     db_set passwd/root-password-crypted ''
  83.     db_set passwd/root-password ''
  84.     db_set passwd/root-password-again ''
  85. fi
  86.  
  87. db_get passwd/make-user
  88. if [ "$RET" = true ] && ! is_system_user; then
  89.     if db_get passwd/user-password-crypted && [ "$RET" ]; then
  90.         USER_PW="$RET"
  91.         USER_PW_CRYPTED=true
  92.     else
  93.         db_get passwd/user-password
  94.         USER_PW="$RET"
  95.         USER_PW_CRYPTED=false
  96.     fi
  97.  
  98.     if db_get passwd/user-uid && [ "$RET" ]; then
  99.         if [ -x $ROOT/usr/sbin/adduser ]; then
  100.             UIDOPT="--uid $RET"
  101.         else
  102.             UIDOPT="-u $RET"
  103.         fi
  104.     else
  105.         UIDOPT=
  106.     fi
  107.  
  108.     # Add the user to the database, using adduser in noninteractive
  109.     # mode.
  110.     db_get passwd/username
  111.     USER="$RET"
  112.     db_get passwd/user-fullname
  113.  
  114.     HOME_EXISTED=
  115.     if [ -d "$ROOT/home/$USER" ]; then
  116.         HOME_EXISTED=1
  117.     fi
  118.  
  119.     if [ -x $ROOT/usr/sbin/adduser ]; then
  120.         $log $chroot $ROOT adduser --disabled-password --gecos "$RET" $UIDOPT "$USER" >/dev/null || true
  121.     else
  122.         $log $chroot $ROOT useradd -c "$RET" -m "$USER" $UIDOPT >/dev/null || true
  123.     fi
  124.  
  125.     # Clear the user password from the database.
  126.     db_set passwd/user-password-crypted ''
  127.     db_set passwd/user-password ''
  128.     db_set passwd/user-password-again ''
  129.     setpassword "$USER" "$USER_PW" "$USER_PW_CRYPTED"
  130.  
  131.     if [ "$HOME_EXISTED" ]; then
  132.         # The user's home directory already existed before we called
  133.         # adduser. This often means that a mount point under
  134.         # /home/$USER was selected in (and thus created by) partman,
  135.         # and the home directory may have ended up owned by root.
  136.         $log $chroot $ROOT chown "$USER:$USER" "/home/$USER" >/dev/null || true
  137.     fi
  138.  
  139.     if [ -n "$USER" ]; then
  140.         for group in lpadmin scanner; do
  141.             $log $chroot $ROOT addgroup --system $group >/dev/null 2>&1 || true
  142.         done
  143.         for group in adm audio cdrom dialout floppy video plugdev dip netdev powerdev lpadmin scanner; do
  144.             $log $chroot $ROOT adduser "$USER" $group >/dev/null 2>&1 || true
  145.         done
  146.         add_to_aliases "$USER"
  147.     fi
  148.  
  149.     db_get passwd/root-login
  150.     if [ "$RET" = false ] && [ -n "$USER" ]; then
  151.         # Ensure sudo is installed, and set up the user to be able
  152.         # to use it.
  153.         if [ ! -e $ROOT/etc/sudoers ]; then
  154.             # try to work in d-i and out; it's better to
  155.             # use apt-install in d-i
  156.             apt-install sudo 2>/dev/null || $log $chroot apt-get -q -y install sudo || true
  157.         fi
  158.         if [ -e $ROOT/etc/sudoers ]; then
  159.             $log $chroot $ROOT addgroup --system admin >/dev/null 2>&1 || true
  160.             $log $chroot $ROOT adduser "$USER" admin >/dev/null 2>&1 || true
  161.             cat <<EOF >>$ROOT/etc/sudoers
  162.  
  163. # Members of the admin group may gain root privileges
  164. %admin ALL=(ALL) ALL
  165. EOF
  166.         else
  167.             # sudo failed to install, system won't be usable
  168.             exit 1
  169.         fi
  170.         # Configure gksu to use sudo, via an alternative, if it's
  171.         # installed and the alternative is registered.
  172.         if $chroot update-alternatives --display libgksu-gconf-defaults >/dev/null 2>&1; then
  173.             $log $chroot update-alternatives --set libgksu-gconf-defaults /usr/share/libgksu/debian/gconf-defaults.libgksu-sudo
  174.             $log $chroot update-gconf-defaults
  175.         fi
  176.     fi
  177. else
  178.     # Just in case, clear any preseeded user password from the database
  179.     # anyway.
  180.     db_set passwd/user-password-crypted ''
  181.     db_set passwd/user-password ''
  182.     db_set passwd/user-password-again ''
  183. fi
  184.  
  185. exit 0
  186.